package dynamicmenus;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import cruds.tires.CrudCreateTire;
import home.Home;
import panels.DynamicMenu;
import panels.MyAjaxLink;
import services.StockableService;

@SuppressWarnings("serial")
public class TiresMenu  extends DynamicMenu{

	List<MyAjaxLink> links = new ArrayList<MyAjaxLink>();
	
	public TiresMenu(Home home,List<MyAjaxLink> links) {
		super(home);
		this.links = links;
		this.setOutputMarkupId(true);
		this.createLinks();
	}
	
	public TiresMenu(Home home) {
		super(home);
		this.setOutputMarkupId(true);
		this.createLinks();
	}

	private void createLinks() {
		StockableService service = this.getHome().getGeneralService().getStockableSer();
		this.createCreateAccessoryButton();
		ListView<MyAjaxLink> listview = new ListView<MyAjaxLink>("listview", this.getLinks()) {
		    protected void populateItem(ListItem<MyAjaxLink> item) {
		        item.add(item.getModelObject());
		    }
		};
		add(listview);
		
		
//		this.add(new MyAjaxLink("pirelli",new TiresList(this.getHome(), service.searchTires("Pirelli"))));
//		this.add(new MyAjaxLink("fate",new TiresList(this.getHome(), service.searchTires("Fate"))));
//		this.add(new MyAjaxLink("firestone",new TiresList(this.getHome(), service.searchTires("Firestone"))));
//		this.add(new MyAjaxLink("bridgestone",new TiresList(this.getHome(), service.searchTires("Bridgestone"))));
//		this.add(new MyAjaxLink("hankook",new TiresList(this.getHome(), service.searchTires("Hankook"))));
//		this.add(new MyAjaxLink("goodrich",new TiresList(this.getHome(), service.searchTires("Goodrich"))));
//		this.add(new MyAjaxLink("goodyear",new TiresList(this.getHome(), service.searchTires("Goodyear"))));
//		this.add(new MyAjaxLink("continental",new TiresList(this.getHome(), service.searchTires("Continental"))));
//		this.add(new MyAjaxLink("yokohama",new TiresList(this.getHome(), service.searchTires("Yokohama"))));
//		this.add(new MyAjaxLink("michelin",new TiresList(this.getHome(), service.searchTires("Michelin"))));
	}

	private void createCreateAccessoryButton() {
		Form form = new Form("form");
        this.add(form);
        form.add(new AjaxButton("crear") {

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				CrudCreateTire page = new CrudCreateTire(TiresMenu.this.getHome());
				page.changePanel(target);
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				
			}
        });
		
	}

	public List<MyAjaxLink> getLinks() {
		return links;
	}

	public void setLinks(List<MyAjaxLink> links) {
		this.links = links;
	}

}
